package com.weizicheng;

import com.weizicheng.dao.UserDao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Calendar;


public class SpringTest {

    @Test
    public void test01(){
        //1.创建spring容器对象, 读取配置文件
        ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.从spring容器中获取bean对象
        //UserDao user = (UserDao) cpac.getBean("user");// 不推荐使用, 因为要强转

        //不推荐使用, 因为当该类型的bean标签对象有多个, 那么就会报错
        UserDao user = cpac.getBean(UserDao.class);
        // UserDao user = cpac.getBean("user", UserDao.class);//推荐使用

        //调用bean对象的方法
        user.save();
    }

    @Test
    public void test2(){
        //需要一个Calendar日历对象
       // Calendar calendar= Calendar.getInstance();

        //通过配置文件来创建对象
        ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
       //静态工厂创建对象
        Calendar calendar = cpac.getBean("calendar", Calendar.class);

        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH)+1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    }
}
